package com.kitty.game.pet.handler;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.bean.PetObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.Random;

import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

@Component("RebuildHandler")
@Slf4j
public class RebuildHandler extends FeedPetHandler {
    private static final String QIANGHUADAN = "宠物强化丹";
    private static final String FAIL_MESSAGE = "#Y{0}#n最高可强化{1}级。";
    private static final String SUCCESS_MESSAGE = "恭喜你，强化成功，#Y{0}#n当前强化等级#R{1}#n级。";
    private static final String FAIL = "真遗憾，强化失败了，#Y{0}#n当前强化进度#R{1}%#n，当进度达到100%强化必定成功。";
    private static final short FULL_PROGRESS = 10000;

    private String canFeed(Pet pet, RoleEquip roleEquip, int num, PetObject petObject) {
        /**野生洗炼必须野生宠物 不是野生就返回*/
        if (isRightType(pet.getType())) {
            return MessageFormat.format(ERROR_PET_TYPE, pet.getName());
        }
        if (petObject == null) {
            log.error("未找到宠物配置=={}", pet.getName());
            return MessageFormat.format(ERROR_PETSET, pet.getName());
        }
        if (!roleEquip.getName().equals(QIANGHUADAN) || roleEquip.queryNum() < num) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, QIANGHUADAN);
        }
        return null;
    }

    /**
     * 洗炼宝宝 只能是宝宝
     */
    @Override
    protected boolean isRightType(byte petType) {
        return petType != BABY;
    }

    private int getAddRate(int rebuildLevel) {
        return (short) (FULL_PROGRESS / Math.pow(rebuildLevel + 3, 2));
    }

    @Override
    public String acceptFeed(Pet pet, Role role, RoleEquip roleEquip) {
        PetObject petObject = PetDataPool.getPetObject(pet.getName());
        String result = canFeed(pet, roleEquip, 1, petObject);
        if (result != null) {
            return result;
        }

        boolean isPhy = pet.getPolar() == 0;
        int currRate;
        int rebuildLevel;
        int maxLevel = petService.getMaxDevelopLevel(pet);
        int addAtt = petService.getRebuildDelta(pet);
        if (isPhy) {
            rebuildLevel = pet.getPhyRebuildLevel();
            currRate = pet.getPhyRebuildRate();
        } else {
            rebuildLevel = pet.getMagRebuildLevel();
            currRate = pet.getMagRebuildRate();
        }

        if (rebuildLevel >= maxLevel) {
            return MessageFormat.format(FAIL_MESSAGE, pet.getName(), maxLevel);
        }
        equipService.delEquipNum(role, roleEquip, 1);
        if (roleEquip.isEverLimit()) {
            petService.addLimitTime(pet, 1);
        }
        currRate += getAddRate(rebuildLevel);
        Random random = new Random();
        int success = random.nextInt(150000);
        if (rebuildLevel == 0 || currRate >= FULL_PROGRESS || (success <= currRate && currRate > 5000)) {
            rebuildLevel++;
            if (isPhy) {
                pet.setPhyRebuildRate(0);
                pet.setPhyRebuildLevel(rebuildLevel);
                pet.setPhyRebuildAdd(pet.getPhyRebuildAdd() + addAtt);
            } else {
                pet.setMagRebuildRate(0);
                pet.setMagRebuildLevel(rebuildLevel);
                pet.setMagRebuildAdd(pet.getMagRebuildAdd() + addAtt);
            }
            pushOperTypeResult(role, true);
            petService.refreshPetInfo(role, pet);
            return MessageFormat.format(SUCCESS_MESSAGE, pet.getName(), rebuildLevel);
        } else {
            if (isPhy) {
                pet.setPhyRebuildRate(currRate);
            } else {
                pet.setMagRebuildRate(currRate);
            }
            petService.refreshPetInfo(role, pet);
            return MessageFormat.format(FAIL, pet.getName(), new DecimalFormat("#.00").format(currRate / 100.0));
        }
    }

}
